﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using BusinessLogic;


namespace Interface.Houses
{
    public partial class City : System.Web.UI.Page
    {
        DataConnection dc = new DataConnection();
        protected void Page_Load(object sender, EventArgs e)
        {
            bindToGrid();
        }

        protected override void OnInit(EventArgs e)
        {
            gridcity.SelectedIndexChanging += new GridViewSelectEventHandler(gridcity_SelectedIndexChanging);
            gridcity.RowEditing += new GridViewEditEventHandler(gridcity_RowEditing);
            gridcity.RowUpdating += new GridViewUpdateEventHandler(gridcity_RowUpdating);
            gridcity.RowCancelingEdit += new GridViewCancelEditEventHandler(gridcity_RowCancelingEdit);
            
        }

        void gridcity_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gridcity.EditIndex = -1;
            bindToGrid();
        }

        void gridcity_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            TextBox tx1 = (TextBox)gridcity.Rows[e.RowIndex].FindControl("txtEditCityName");
            string city = tx1.Text;
            //bool txtcheck = checkTextBox(tx1);
            if (tx1.Text==string.Empty)
            {
                lblMS.Text = "Nhập vào tên thành phố";

                return;
            }
            else
            {

                bool citycheck = dc.checkCity(city);
                if (citycheck == true)
                {

                    int id = (int)gridcity.DataKeys[e.RowIndex].Value;
                    
                    string updateQuery = "update City set Name = N'" + city + "' where IdCity = '" + id + "'";

                    dc.ExecNonQuery(updateQuery);

                    bindToGrid();
                    lblMS.Text = "cap nhat thanh cong";
                }
                else
                {
                    lblMS.Text = "Thành Phố Đã Tồn Tại";

                    return;
 
                }

                gridcity.EditIndex = -1;
                bindToGrid();

            }

        }

        void gridcity_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gridcity.EditIndex = e.NewEditIndex;
        }

        void gridcity_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            int id = (int)gridcity.DataKeys[e.NewSelectedIndex].Value;
            Response.Redirect("UserDetails.aspx?ID=" + id);
        }

        protected void bindToGrid()
        {
            string strsql = "Select * from City";
            DataTable dt = dc.getData(strsql);
            DataTable dtUser = dataTableInitiate();
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    int stt = i + 1;
                    DataRow row = dt.Rows[i];
                    int ID = Convert.ToInt32(row["IdCity"].ToString());
                    string cityName = row["Name"].ToString();
                    
                    DataRow dtRow = dtUser.NewRow();
                    dtRow["ID"] = ID;
                    dtRow["STT"] = stt;
                    dtRow["Name"] = cityName;
                    
                    dtUser.Rows.Add(dtRow);
                }
            }

            gridcity.DataSource = dtUser;
            gridcity.DataBind();
        }

        protected DataTable dataTableInitiate()
        {
            DataTable dt = new DataTable();
            DataColumn col = dt.Columns.Add("ID", typeof(int));
            col.AutoIncrement = true;
            col.AutoIncrementStep = 1;
            col.AutoIncrementSeed = 1;
            dt.Columns.Add("STT", typeof(string));
            dt.Columns.Add("Name", typeof(string));

            
            return dt;
        }

        protected void Add_Click(object sender, EventArgs e)
        {
            bool checktext = checkTextBox(cityName);

            if (checktext == false)
            {
                lblMS.Text = "Nhập tên thành phố vào";
                
                return;
            }

            string cityname = cityName.Text;

            BusinessLogic.DataConnection dc = new BusinessLogic.DataConnection();
            bool check = dc.checkCity(cityname);

            if (check == false)
            {
                lblMS.Text = "Thành phố đã tồn tại";
                return;
            }

            else
            {
                string insertQuery = "insert into City values (N'" + cityname + "')";


                dc.ExecNonQuery(insertQuery);

                bindToGrid();

            }
        }


        bool checkTextBox(params TextBox[] txts)
        {
            bool isFilled = true;
            foreach (TextBox txt in txts)
            {
                if (txt.Text.Length == 0)
                {
                    isFilled = false;
                }
            }
            return isFilled;
        }


    }
}